package helloworld.admin.service.center;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import helloworld.dao.CenterDao;
import helloworld.dao.DAOException;
import helloworld.dao.DAOFactory;
import helloworld.example.exception.ErrorLevel;
import helloworld.exception.DuplicateCenterException;

import helloworld.admin.bean.*;

/**
 * In real projects, CustomerControllerService object is often implemented as Session EJB.
 * It represents a Business Service and is Session Facade pattern (Core J2EE Patterns) 
 * 
 * @author TUYEN-DM
 * @version $Revision:   $ $Date:20090424   $
 */
public class CenterControllerService {

	public CenterAdminBean getCenterDetail(String centerName) throws CenterServiceException
	{
		CenterAdminBean center = null;
		try{
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			CenterDao dao = factory.getCenterDAO();
			center = dao.fetchDetail(centerName);
			
		}catch (DAOException de)
		{
			if (! de.isLogged())
			{
				Log log = LogFactory.getLog("CenterFetchService");
				de.log(log);
			}
			throw new CenterServiceException(de.getUserMessageKey(), 
									de.getValueReplacementArray(),
									de.getUniqueID(), 
									ErrorLevel.ERROR);
		}
		
		return center;
	}
	
	public int createCenter(CenterAdminBean center) throws CenterServiceException, DuplicateCenterException{
		try{
			/*center.getAddress().setAddress1("N/A");
			center.getAddress().setAddress2("N/A");*/
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			CenterDao dao = factory.getCenterDAO();
			return dao.create(center);
		}catch (DAOException de) {
			// TODO: handle exception
			if(!de.isLogged()){
				Log log = LogFactory.getLog("CenterInsertService");
				de.log(log);
			}
			throw new CenterServiceException(de.getUserMessageKey(),de.getValueReplacementArray(),
												de.getUniqueID(),ErrorLevel.ERROR);
		}
	}
	
	public void updateCenter(CenterAdminBean center)throws CenterServiceException,DuplicateCenterException{
		try{
			/*center.getAddress().setAddress1("N/A");
			center.getAddress().setAddress2("N/A");*/
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			CenterDao dao = factory.getCenterDAO();
			dao.update(center);
		}catch (DAOException de) {
			// TODO: handle exception
			if(!de.isLogged()){
				Log log = LogFactory.getLog("CenterUpdateService");
				de.log(log);
			}
			throw new CenterServiceException(de.getUserMessageKey(),de.getValueReplacementArray(),
												de.getUniqueID(),ErrorLevel.ERROR);
		}
	}
	
	public void deleteCenter(int id) throws CenterServiceException{
		try{
			
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			CenterDao dao = factory.getCenterDAO();
			dao.delete(id);
		}catch (DAOException de) {
			// TODO: handle exception
			if(!de.isLogged()){
				Log log = LogFactory.getLog("CenterDeleteService");
				de.log(log);
			}
			throw new CenterServiceException(de.getUserMessageKey(),de.getValueReplacementArray(),
												de.getUniqueID(),ErrorLevel.ERROR);
		}
	}
}
